import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/vo/common_vo/common_pagination_vo.dart';
import 'package:xinxuan/vo/seckill_recovery_vo.dart';

class RecoveryPageController extends GetxController {

  /// tabIndex 
  var tabIndex = 1.obs;
  List<int> statusArr = [8, 1];
  /// 数据
  var dataArr = <SeckillRecoveryVo>[
    SeckillRecoveryVo(data: [], meta: SeckillRecoveryMeta(isRecovery: 0, pagination: CommonPagination(total: 0, currentPage: 1))),
    SeckillRecoveryVo(data: [], meta: SeckillRecoveryMeta(isRecovery: 0, pagination: CommonPagination(total: 0, currentPage: 1)))
  ].obs;
  
  @override
  void onReady() {
    getSeckillRecoveryList();
    super.onReady();
  }

  /// 获取某项列表数据
  getSeckillRecoveryItemList() async {
    dataArr[tabIndex.value].meta!.pagination!.currentPage = 1;
    SeckillRecoveryVo tempVo = await HttpUtilService.getSeckillRecoveryList(status: statusArr[tabIndex.value], page: dataArr[tabIndex.value].meta!.pagination!.currentPage!, loading: false);
    dataArr[tabIndex.value] = tempVo;
    update();
  }

  /// 获取回收列表
  getSeckillRecoveryList() async {
    EasyLoading.show();
    for (int i = 0; i < dataArr.length; i++) {
      SeckillRecoveryVo tempVo = await HttpUtilService.getSeckillRecoveryList(status: statusArr[i], page: dataArr[i].meta!.pagination!.currentPage!, loading: false);
      dataArr[i] = tempVo;
    }
    EasyLoading.dismiss();
    update();
  }

  /// 加载更多回收列表
  onloadSeckillRecoveryList() async {    
    if (dataArr[tabIndex.value].meta!.pagination!.total! <= dataArr[tabIndex.value].data!.length) {
      return;
    }
    dataArr[tabIndex.value].meta!.pagination!.currentPage = dataArr[tabIndex.value].meta!.pagination!.currentPage! + 1;
    SeckillRecoveryVo tempVo = await HttpUtilService.getSeckillRecoveryList(status: statusArr[tabIndex.value], page: dataArr[tabIndex.value].meta!.pagination!.currentPage!, loading: false);
    dataArr[tabIndex.value].data!.addAll(tempVo.data!);
    update();
  }

  /// 回收
  recoveryProducts(int id) async {
    var res = await HttpUtilService.recoveryProducts(id: id);
    if (res) {
      EasyLoading.showToast('回收成功');
      dataArr.forEach((item) {
        item.data!.forEach((seckillRecoveryItem) {
          if (seckillRecoveryItem.id == id) {
            seckillRecoveryItem.status = 8;
            seckillRecoveryItem.statusStr = '已回收';
          }
        });
      });
      update();
    }
  }

}